package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@WebServlet("/xiazai1")
public class xiazai1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        String name1 = request.getParameter("name");
        System.out.println(name1);
        response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(name1,"utf-8"));
        InputStream in = getServletContext().getResourceAsStream(name1);
        ServletOutputStream out = response.getOutputStream();

        int len =0;
        byte[] arr =new byte[1024*8];
        while((len=in.read(arr))!=01){
            out.write(arr,0,len);
        }


    }
}
